package com.cloudsigns.factorymanager2.produce.activity.produce_team;

import android.content.Intent;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cloudsigns.factorymanager2.R;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartBean;
import com.cloudsigns.factorymanager2.produce.bean.WorkPartOrderResponse;
import com.cloudsigns.factorymanager2.produce.fragment.produce_team.ProduceTeamOrderDailyFeedbackFragment;
import com.cloudsigns.factorymanager2.produce.fragment.produce_team.ProduceTeamOrderInformationFragment;
import com.cloudsigns.factorymanager2.produce.fragment.produce_team.ProduceTeamOrderWorkpartHandoverFragment;
import com.cloudsigns.factorymanager2.produce.iview.produce_team.IWorkPartDetailView;
import com.cloudsigns.factorymanager2.produce.presenter.produce_team.WorkPartDetailPresenter;
import com.cloudsigns.factorymanager2.produce.utils.ProduceForTeamEvent;
import com.cloudsigns.factorymanager2.project.adapter.ProjectMainViewPageAdapter;
import com.cloudsigns.factorymanager2.project.customview.CustomViewPager;
import com.cloudsigns.factorymanager2.project.customview.PageIndicatorView;
import com.cloudsigns.factorymanager2.project.utils.ConstantUtil;

import java.util.ArrayList;

import de.greenrobot.event.EventBus;

public class ProduceTeamOrderDetailFragmentActivity extends FragmentActivity implements View.OnClickListener, IWorkPartDetailView {
    private final String TAG = ProduceTeamOrderDetailFragmentActivity.class.getSimpleName();
    private RelativeLayout rl_back;
    private TextView tv_title;
    private View ll_order_information, ll_daily_feedback, ll_process_handover;
    private TextView tv_order_information, tv_daily_feedback, tv_process_handover;
    private CustomViewPager viewpager;
    private View rl_progressbar;
    public static final int ORDER_INFORMATION = 0;
    public static final int DAILY_FEEDBACK = 1;
    public static final int PROCESS_HANDOVER = 2;
    public int currentIndex = ORDER_INFORMATION;

    private ProduceTeamOrderInformationFragment informationFragment;
    private ProduceTeamOrderDailyFeedbackFragment dailyFeedbackFragment;
    private ProduceTeamOrderWorkpartHandoverFragment processHandoverFragment;
    private ArrayList<Fragment> listFragments;
    private ProjectMainViewPageAdapter adapter;

    private WorkPartOrderResponse workPartOrderResponse;
    private WorkPartDetailPresenter workPartDetailPresenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
        // 沉浸式状态栏
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Window window = getWindow();
            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
        }
        setContentView(R.layout.layout_produce_team_order_detail);

        initView();

        tv_title.setText(getResources().getText(R.string.produce_team1));

        workPartDetailPresenter = new WorkPartDetailPresenter(this);

        Intent intent = getIntent();
        if (null != intent) {
            workPartOrderResponse = (WorkPartOrderResponse) intent.getSerializableExtra(ConstantUtil.KEY_1);
        }

        getWorkPartDetail();


        EventBus.getDefault().register(this);// 注册
    }

    private void initView() {
        rl_back = findViewById(R.id.rl_back);
        tv_title = findViewById(R.id.tv_title);
        rl_progressbar = findViewById(R.id.rl_progressbar);
        viewpager = (CustomViewPager) findViewById(R.id.viewpager);
        ll_order_information = findViewById(R.id.ll_order_information);
        ll_daily_feedback = findViewById(R.id.ll_daily_feedback);
        ll_process_handover = findViewById(R.id.ll_process_handover);
        tv_order_information = findViewById(R.id.tv_order_information);
        tv_daily_feedback = findViewById(R.id.tv_daily_feedback);
        tv_process_handover = findViewById(R.id.tv_process_handover);

        rl_back.setOnClickListener(this);
        ll_order_information.setOnClickListener(this);
        ll_daily_feedback.setOnClickListener(this);
        ll_process_handover.setOnClickListener(this);

        informationFragment = new ProduceTeamOrderInformationFragment();
        dailyFeedbackFragment = new ProduceTeamOrderDailyFeedbackFragment();
        processHandoverFragment = new ProduceTeamOrderWorkpartHandoverFragment();

        listFragments = new ArrayList<Fragment>();
        listFragments.add(informationFragment);
        listFragments.add(dailyFeedbackFragment);
        listFragments.add(processHandoverFragment);

        adapter = new ProjectMainViewPageAdapter(getSupportFragmentManager(), listFragments);
        viewpager.setAdapter(adapter);
        viewpager.setOffscreenPageLimit(2);

        PageIndicatorView indicator = (PageIndicatorView) findViewById(R.id.indicator);
        indicator.setViewPager(viewpager);
        indicator.setFades(false);
        indicator.setSelectedColor(getResources().getColor(R.color.rgb_2E75B6));
        indicator.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

            @Override
            public void onPageSelected(int position) {
                showSelectedOption(position);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {
            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    /**
     * 切换到哪个tab页
     *
     * @param pos
     */
    private void showSelectedOption(int pos) {
        // TODO Auto-generated method stub
        switch (pos) {
            // 工单信息
            case ORDER_INFORMATION:
                ll_order_information.setSelected(true);
                ll_daily_feedback.setSelected(false);
                ll_process_handover.setSelected(false);

                tv_order_information.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_daily_feedback.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_process_handover.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;
            // 日反馈
            case DAILY_FEEDBACK:
                ll_order_information.setSelected(false);
                ll_daily_feedback.setSelected(true);
                ll_process_handover.setSelected(false);

                tv_order_information.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_daily_feedback.setTextColor(getResources().getColor(R.color.rgb_3333333));
                tv_process_handover.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                break;
            // 工序交接
            case PROCESS_HANDOVER:
                ll_order_information.setSelected(false);
                ll_daily_feedback.setSelected(false);
                ll_process_handover.setSelected(true);

                tv_order_information.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_daily_feedback.setTextColor(getResources().getColor(R.color.color_a2a2a4));
                tv_process_handover.setTextColor(getResources().getColor(R.color.rgb_3333333));
                break;
            default:
                break;
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.rl_back:
                finish();
                break;
            // 工单信息
            case R.id.ll_order_information:
                if (currentIndex != ORDER_INFORMATION) {
                    currentIndex = ORDER_INFORMATION;
                    viewpager.setCurrentItem(ORDER_INFORMATION);
                    showSelectedOption(ORDER_INFORMATION);
                }
                break;
            // 日反馈
            case R.id.ll_daily_feedback:
                if (currentIndex != DAILY_FEEDBACK) {
                    currentIndex = DAILY_FEEDBACK;
                    viewpager.setCurrentItem(DAILY_FEEDBACK);
                    showSelectedOption(DAILY_FEEDBACK);
                }
                break;
            //　工序交接
            case R.id.ll_process_handover:
                if (currentIndex != PROCESS_HANDOVER) {
                    currentIndex = PROCESS_HANDOVER;
                    viewpager.setCurrentItem(PROCESS_HANDOVER);
                    showSelectedOption(PROCESS_HANDOVER);
                }
                break;
        }
    }

    public void onEventMainThread(ProduceForTeamEvent event) {
        getWorkPartDetail();
    }

    private void getWorkPartDetail(){
        if (null != workPartDetailPresenter)
            workPartDetailPresenter.workPartDetail();
    }

    @Override
    public String getOrderId() {
        return workPartOrderResponse.getOrderId();
    }

    @Override
    public String getProductId() {
        return workPartOrderResponse.getProductId();
    }

    @Override
    public String getWorkPartId() {
        return workPartOrderResponse.getWorkPartId();
    }

    @Override
    public String getWorkPartOrderId() {
        return workPartOrderResponse.getWorkPartOrderId();
    }

    @Override
    public void workPartDetailSueecssed(WorkPartBean workPartBean) {
        if (null != workPartBean) {
            informationFragment.initData(workPartBean, workPartOrderResponse);
            dailyFeedbackFragment.initData(workPartBean, workPartOrderResponse);
        }
    }

    @Override
    public void workPartDetailFailed(final String desc) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Toast.makeText(ProduceTeamOrderDetailFragmentActivity.this, desc, Toast.LENGTH_LONG).show();
            }
        });
    }

    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);//解注册
        setResult(ConstantUtil.START_ACTIVITY_KEY_1, null);
    }
}
